<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/style.css">
    <style>
        table{
            width: 100%;
            margin-bottom: 15px;
            border-collapse: collapse;
            table-layout: fixed;
        }
        th,td{
            border: 1px solid #CBD6DE;
            padding-left: 10px;
            line-height: 28px;
        }
        th{
            text-align: left;
            background: linear-gradient(#edf2f5,#dce9f2,#edf2f5);
            color:#467aa7;
        }
        tbody tr:nth-child(even){
            background: #f2f2f2;
        }
        #pages{
            text-align: center;
            padding-top: 8px;
        }
        .page{
            min-width: 50px;
            display: inline-block;
        }
    </style>
</head>
<body>
<div id="app">
	<div id="wrap">
	    <div id="header">
	        <div style="float: right;padding-top: 24px">
				 <span style="margin-right: 50px;" v-model="username"> 欢迎您：{{admin.data.username}}</span>
				<span style="margin-right: 10px;">
				     <script type="text/javascript">
				       var date = new Date();
				       document.write("今天是：" + date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日" + " 星期" + "日一二三四五六".charAt(date.getDay()));
				     </script>
				</span>
	            <a href="../login.html" style="color:#fff;float: right">安全退出</a>
	        </div>
	        <h1>旅游信息管理系统</h1>
	    </div>
	    <div id="header-bar"></div>
	    <div id="content" style="height: 360px">
	        <h2>省份列表</h2>
	        <table>
	            <thead>
	            <tr>
	                <th width="15%">ID</th>
	                <th width="20%">省份</th>
	                <th width="25%">标签</th>
	                <th width="15%">景点个数</th>
	                <th width="25%">操作</th>
	            </tr>
	            </thead>
				
	            <tbody>
	            <tr v-for="province in provinces">
	                <td v-text="province.id"></td>
	                <td v-text="province.name"></td>
	                <td v-text="province.tags"></td>
	                <td v-text="province.placecounts"></td>
	                <td>
	                    <a href="javascript:;" @click="deleteProvince(province.id)">删除省份</a>
	                    <a :href="'../viewspot/viewspotlist.html?provinceid='+province.id">景点列表</a>
	                    <a :href="'updateprovince.html?provinceid='+province.id">修改省份</a>
	                </td>
	            </tr>
	           
	            </tbody>
	        </table>
	        <a href="addprovince.html">
	            <button type="button">添加省份</button>
	        </a>
	        <div id="pages">
	            <a href="javascript:;" v-if="page>1" class="page" @click="findAll(page-1)">&lt;上一页</a>
				<a href="javascript:;" v-if="page==1" class="page">&lt;上一页</a>
	            <a href="javascript:;" v-for="indexpage in totalPage" v-text="indexpage" @click="findAll(indexpage)"></a>
				<a href="javascript:;" v-if="page==totalPage" class="page">下一页&gt;</a>
	            <a href="javascript:;" v-if="page<totalPage" class="page" @click="findAll(page+1)">下一页&gt;</a>
	        </div>
	    </div>
	    <div id="footer">
	        ABC@126.com
	    </div>
	</div>
</div>
</body>
</html>
<script src="../js/vue.js"></script>
<script src="../js/axios.min.js"></script>
<script>
    const app = new Vue({
        el:"#app",
        data:{
            provinces:{},
			page:1,
			rows:4,
			totalPage:0,
			totals:0,
			username:'',
			admin:''
           
        },
        methods:{
            findAll(indexpage){//查询所有
				if(indexpage){
					this.page = indexpage;
				}
                _this = this;
                axios.get("http://localhost:8080/province/allProvince?page="+this.page+"&size="+this.rows).then((res)=>{
                    //console.log(res.data)
					_this.provinces = res.data.province;
					_this.totalPage = res.data.totalPage;
					_this.totals = res.data.totals;
                    
                });
            },
			deleteProvince(id){
				if(confirm("你确定要删除省份吗？")){
					axios.get("http://localhost:8080/province/delete?id="+id).then((res)=>{
						//console.log("id"+id);
					    if(res.data.status){
							alert(res.data.msg);
							location.href='provincelist.html';
							
							
						}else{
							alert(res.data.msg);
						}
					    
					});
				}	 
			}
        
        },
        created(){
           
			//this.username = localStorage.getItem("username");
			this.admin =JSON.parse(window.localStorage.getItem("access-admin"));
			console.log(this.admin)
			console.log(this.admin.data.username)
			if(!this.admin){
				window.location='../login.html';
			}else{
				//检验token合法性
				axios({
					url:'http://localhost:8080/user/checkToken',
					method:'get',
					headers:{
						token:this.admin.data.token
					}
				}).then((res) => {
					if(res.data.status){
						//展示数据
						 this.findAll();
					}else{
						//提示信息并返回登录页面
						alert(res.data.msg);
						location.href='../login.html'
					}
				})
			}
        }
    })
</script>